#
# Kurella/Rapp: The role of issue salience and competitive advantages in spatial models of political competition
#
# Replication Material: Table 2-33 Online Appendix
#
#
######################################

################################################################################
#
# Load required Packages 
#
library(dplyr)
library(survival)
library(stargazer)
library(haven)

################################################################################
#
# Read data
#

setwd("P:/R-Codes/Niche Parties/Replication Material JOP")
dlong <- read_dta("ReplicationData_clogit.dta")


################################################################################
#
# Calculation of conditional logit model for each country and EVS wave
#

dlong <- as_tibble(dlong)
mvars <- select(dlong, c( party_A_choice , party_A_green,party_A_right ,
                          partyB , partyC , partyD , partyE , partyF , partyG , partyH ,partyI , partyJ , partyK , partyL , partyM , partyN , partyO , partyP, 
                          pdummy,dist2econ_index  , dist2moral_index , dist2envir_index ,dist2gen_index , dist2immi_index ,
                          lfdn, cntry_wave))


# Estimate salience based on conditional logit models
reslist <- NULL

for(i in unique(dlong$cntry_wave) ){
  print(i)
  # models for countries with green and right parties
  if(any(dlong$party_A_green[dlong$cntry_wave==i]==1) & any(dlong$party_A_right[dlong$cntry_wave==i]==1))
  {  datamat <- mvars[mvars$cntry_wave==i,]
  keep <- rep(FALSE,26)
  for(j in 4:18){
    keep[j] <- ifelse(sum(datamat[,j], na.rm=TRUE)==0, FALSE, TRUE)
  }
  
  xnam <- names(datamat)[keep]
  form <- as.formula(paste(paste("party_A_choice ~ party_A_green +  party_A_right + ", 
                                 paste(xnam, collapse= "+")), 
                           "+ dist2econ_index  + dist2moral_index + dist2envir_index + dist2gen_index + dist2immi_index +
                       strata(lfdn)", sep=""))
  
  model_all <-clogit(form,
                     data=datamat, method="efron")}
  
  # models for countries with green party
  if(any(dlong$party_A_green[dlong$cntry_wave==i]==1) & 
     all(dlong$party_A_right[dlong$cntry_wave==i]==0)){
    
    datamat <- mvars[mvars$cntry_wave==i,]
    keep <- rep(FALSE,26)
    for(j in 4:18){
      keep[j] <- ifelse(sum(datamat[,j], na.rm=TRUE)==0, FALSE, TRUE)   }
    xnam <- names(datamat)[keep]
    form <- as.formula(paste(paste("party_A_choice ~ party_A_green +  ", 
                                   paste(xnam, collapse= "+")), 
                             "+ dist2econ_index  + dist2moral_index + dist2envir_index + dist2gen_index + dist2immi_index +
                       strata(lfdn)", sep=""))
    
    model_all <-clogit(form,
                       data=dlong[dlong$cntry_wave==i,], method="efron")}
  
  # models for countries with right parties
  if(all(dlong$party_A_green[dlong$cntry_wave==i]==0) &
     any(dlong$party_A_right[dlong$cntry_wave==i]==1)){
    
    datamat <- mvars[mvars$cntry_wave==i,]
    
    xnam <- names(datamat)[keep]
    form <- as.formula(paste(paste("party_A_choice ~ party_A_right +  ", 
                                   paste(xnam, collapse= "+")), 
                             "+ dist2econ_index  + dist2moral_index + dist2envir_index + dist2gen_index + dist2immi_index +
                       strata(lfdn)", sep=""))
    
    model_all <-clogit(form,
                       data=dlong[dlong$cntry_wave==i,], method="efron")}
  
  
  # models for countries without green and right parties
  if(all(dlong$party_A_green[dlong$cntry_wave==i]==0) &
     all(dlong$party_A_right[dlong$cntry_wave==i]==0)){
    
    datamat <- mvars[mvars$cntry_wave==i,]
    keep <- rep(FALSE,26)
    for(j in 4:18){
      keep[j] <- ifelse(sum(datamat[,j], na.rm=TRUE)==0, FALSE, TRUE)   }
    xnam <- names(datamat)[keep]
    form <- as.formula(paste(paste("party_A_choice ~   ", 
                                   paste(xnam, collapse= "+")), 
                             "+ dist2econ_index  + dist2moral_index + dist2envir_index + dist2gen_index + dist2immi_index +
                       strata(lfdn)", sep=""))
    
    model_all <-clogit(form,
                       data=dlong[dlong$cntry_wave==i,], method="efron")}
  
  print(summary(model_all))
  #############################################################################################################
  # Online Appendix Table 2 - Table 33: regression tables for country wave results of conditional logit models  
  stargazer(model_all,  type="latex", title="Results of conditional logit for ",
            dep.var.labels=c("Vote choice"),
            omit.stat=c("LL","ser","f"),   single.row=TRUE)
  
}
